/* 
 * Copyright (c) Sergey B. Berezin (s_berezin@cs.msu.su), 2002, 2003.
 *
 * You may freely distribute or modify this source code retaining the 
 * above copyright notice.
 *
 * This software is provided "as-is" and without guarantee or warrantee 
 * of any kind, expressed or implied :)
 */

using OpenGL;
using System.Drawing;

namespace UVS.UI {

	public class BackgroundLayer : Layer {

		protected Color clearColor;
		protected GL.Buffers clearBuffers;

		public BackgroundLayer() {
			clearColor = Color.Black;
			clearBuffers = GL.Buffers.Color;
		}

		public override void OnRender() {
			if(view.Context.Width == view.Width &&
			   view.Context.Height == view.Height) {
				GL.glClearColor(clearColor);  // User fast clear functions only if
				GL.glClear(clearBuffers);     // layer's size matches context size
			} else {
				GL.glMatrixMode(GL.GL_MODELVIEW);
				GL.glPushMatrix();
				GL.glLoadIdentity();
				GL.glMatrixMode(GL.GL_PROJECTION);
				GL.glPushMatrix();
				GL.glLoadIdentity();
				GL.glOrtho(0,1,0,1,-1,1.0001);

				GL.glDisable(GL.GL_DEPTH_TEST);  // TODO: add code to clear 
				GL.glDisable(GL.GL_CULL_FACE);   // rectangular area of depth buffer
				GL.glDisable(GL.GL_TEXTURE_2D);
				GL.glPolygonMode(GL.GL_FRONT_AND_BACK,GL.GL_FILL);

				GL.glBegin(GL.GL_QUADS);
				GL.glColor(clearColor);
				GL.glVertex3d(0,0,1);
				GL.glVertex3d(1,0,1);
				GL.glVertex3d(1,1,1);
				GL.glVertex3d(0,1,1);
				GL.glEnd();

				GL.glPopMatrix();
				GL.glMatrixMode(GL.GL_MODELVIEW);
				GL.glPopMatrix();
			}
		}

		public Color Color {
			get {
				return clearColor;
			}
			set {
				clearColor = value;
				if(view != null)
					view.UpdateAllLayers();
			}
		}

		public GL.Buffers Buffers {
			get {
				return clearBuffers;
			}
			set {
				clearBuffers = value;
				if(view != null)
					view.UpdateAllLayers();
			}
		}
	}
}